<#include  "/common/csharp.ftl">
namespace pos.entity
{
<#list doc.entitymodel.entity as entity>
<#if entity.@entity_name="${object()}">
using System;
using System.Data;
using System.Configuration;
using System.Text;
using System.Data.OleDb;
using System.Collections;
using System.Windows.Forms;
using NHibernate;
using NHibernate.Criterion;
/// <summary>
/// t_${entity.@entity_name} 的摘要说明
/// </summary>
public class t_${entity.@entity_name}:IEntityUtil
{
	<#list entity.field as field>
<#if (field.consts?if_exists?size>0)>
//   ${field.@label}值的哈希表 
	private static Hashtable m_enum_${field.@name};
	public static Hashtable  enum_${field.@name}
	{
		get{return m_enum_${field.@name};}
	}
<#list field.consts.const as enum>
	public const <@fieldToCSharpType field=field/> ${enum.@name}=<@fieldAtrributeToFieldValue field=field>${enum.@value}</@fieldAtrributeToFieldValue>;
</#list>
</#if>
	</#list>
	//   列名称哈希表 
	private static Hashtable m_colLabels;    	 
	public static Hashtable colLabels//
	{
		get{return m_colLabels;}
	}
	//表字段名称哈希表
	private static ArrayList m_colNames;     
	public static ArrayList  colNames　
	{
		get{return m_colNames;}
	}
	private ${entity.@entity_name?cap_first} __${entity.@entity_name};
	<#list entity.field as field>
	public <@fieldToCSharpType field=field/> ${field.@name}  
	{
		get{return __${entity.@entity_name}.${field.@name};}
		set{ __${entity.@entity_name}.${field.@name}=value;}
	}
	</#list>
	private static  ${entity.@entity_name?cap_first}Dao dao=(${entity.@entity_name?cap_first}Dao)SpringContext.Context.GetObject("${entity.@entity_name?cap_first}Dao");
	public static ${entity.@entity_name?cap_first}Dao DAO
	{
		get{return dao;}
		
	}
	
	public t_${entity.@entity_name}()
	{
		__${entity.@entity_name}=new ${entity.@entity_name?cap_first}();
		<#list entity.field as field>
	  	${field.@name}=<@fieldValueInit field=field></@fieldValueInit>;
		</#list>
	  // dao=(${entity.@entity_name?cap_first}Dao)SpringContext.Context.GetObject("${entity.@entity_name?cap_first}Dao");
	   if(m_colLabels==null)
	   {
		m_colLabels = new Hashtable();
        m_colNames= new ArrayList();
		<#list entity.field as field>
		m_colLabels.Add("${field.@name}", @"${field.@label}");
		colNames.Add("${field.@name}");
		<#if (field.consts.const?if_exists?size>0) >
		m_enum_${field.@name}= new Hashtable();
		<#list field.consts.const as enum>
		m_enum_${field.@name}.Add("${enum.@value}", @"${enum.@label}");
		</#list>
		</#if>
		</#list>	
	  }
	
	}
	public t_${entity.@entity_name}(${entity.@entity_name?cap_first} entity )
	{
		__${entity.@entity_name}=entity;
		
	  // dao=(${entity.@entity_name?cap_first}Dao)SpringContext.Context.GetObject("${entity.@entity_name?cap_first}Dao");
	   if(m_colLabels==null)
	   {
		m_colLabels = new Hashtable();
        m_colNames= new ArrayList();
		<#list entity.field as field>
		m_colLabels.Add("${field.@name}", @"${field.@label}");
		colNames.Add("${field.@name}");
		<#if (field.consts.const?if_exists?size>0) >
		m_enum_${field.@name}= new Hashtable();
		<#list field.consts.const as enum>
		m_enum_${field.@name}.Add("${enum.@value}", @"${enum.@label}");
		</#list>
		</#if>
		</#list>	
	  }
	
	}
	public Object uniqueResult(DetachedCriteria dc)
    {
        return dao.uniqueResult(dc);
    }
	 public DetachedCriteria DC 
     {
        	get{return DetachedCriteria.For(typeof(${entity.@entity_name?cap_first}));}
     }
	<#if (entity.@seqID?if_exists?size>0) >
	public const string ENTITYNAME="t_${entity.@entity_name}";
	public const int SEQN0=${entity.@seqID};
	
	</#if>
    public void checkAdd()
    {
		int len;
<#list entity.field as field>
		<@checkFieldValue entity=entity field=field  ></@checkFieldValue>
</#list>
//unique="yes" 唯一性验证
<#assign c=1 />
<#list entity.field as field>
<#if (field.@unique?if_exists?size>0) && (field.@unique="yes") && (entity.prim_key.@field!=field.@name)>
	<#if (c>1)>
		crit=Restrictions.Or(crit,Restrictions.Eq("${field.@name}", ${field.@name}));
	<#else>
		ICriterion crit=Restrictions.Eq("${field.@name}", ${field.@name});
	</#if>
	<#assign c=c+1 />
</#if>
</#list>
<#if (c>1)>
  		IList list=dao.List(dao.DC.Add(crit));
		if(list.Count==0)
			return;
		${entity.@entity_name?cap_first} obj=list[0] as ${entity.@entity_name?cap_first};
//唯一性异常消息抛出
<#list entity.field as field>
<#if (field.@unique?if_exists?size>0) && (field.@unique="yes") && (entity.prim_key.@field!=field.@name)>
		if( obj.${field.@name}==${field.@name})
		{
			throw (new Exception(string.Format("${field.@label}:{0}已经存在",${field.@name})));	
		}
</#if>
</#list>		
	//end unique="yes" 唯一性验证
</#if>
    }
    public void checkUpdate()
    {
		int len;
<#list entity.field as field>
		<@checkFieldValue entity=entity field=field  ></@checkFieldValue>
</#list>
//unique="yes" 唯一性验证
<#assign c=1 />
<#list entity.field as field>
<#if (field.@unique?if_exists?size>0) && (field.@unique="yes")  >
	<#if (c>1)>
		crit=Restrictions.Or(crit,Restrictions.Eq("${field.@name}", ${field.@name}));
	<#else>
		ICriterion crit=Restrictions.Eq("${field.@name}", ${field.@name});
	</#if>
	<#assign c=c+1 />
</#if>
</#list>
<#if (c>1)>
  		IList list=dao.List(dao.DC.Add(crit));
		if(list.Count==1)
			return;
		if(list.Count==0)
			throw (new Exception("该记录已经被删除"));	
		else
			throw (new Exception("改纪录出现重名!"));
</#if>	
    }
    public void add()
    {
        try
        {
            checkAdd();
         	dao.Add(__${entity.@entity_name});
        }
        catch(Exception ex)
        {
            throw (new Exception(ex.Message));
        }
    }
   
    public void delBy<#list entity.prim_key as primkey>${primkey.@field}</#list>()
    {
    
        try
        {
          dao.Delete(__${entity.@entity_name});
        }
        catch(Exception ex)
        {
            throw (new Exception(ex.Message));
        }
    }

    public  void update()
    {
        try
        {
            checkUpdate();
            dao.Update(__${entity.@entity_name});
        }
        catch (Exception ex)
        {
            throw (new Exception(ex.Message));
        }
    }
   
    public void ExecDataUpdate(int rowNo)
    {
		if(m_updateRow==null)
			return;
		int ii=0;
    <#assign i=0>
<#list entity.field as field>
<#if (field.@show?if_exists?size>0)>
     <#if (field.show?if_exists?size>0)>
		<#list field.show as show>
		${show.@entity_name?cap_first} p${i} =((${show.@entity_name?cap_first}Dao)SpringContext.Context.GetObject("${show.@entity_name?cap_first}Dao")).Get(
		((${show.@entity_name?cap_first}Dao)SpringContext.Context.GetObject("${show.@entity_name?cap_first}Dao")).DC.Add(Restrictions.Eq("${show.@ref}",${field.@name})));
		
		if(p${i}==null)
			 m_updateRow(rowNo,ii++,<@fieldValueToString entity=entity field=field>${field.@name}</@fieldValueToString>);
		else
			m_updateRow(rowNo,ii++,p${i}.${show.@refvalue}.ToString());
		<#assign i=i+1>
		<#break>
		</#list>
     <#else>m_updateRow(rowNo,ii++,<@fieldValueToString entity=entity field=field>${field.@name}</@fieldValueToString>);	
     </#if>
</#if>     
</#list>

    }
    public void ExecHeadInsert()
    {
		if(m_insertHead!=null)
        {
			int ii=0;
			<#list entity.field as field>
	<#if (field.@show?if_exists?size>0)>
			m_insertHead(ii++,${field.@show},"${field.@label}");
	</#if>
			</#list>
		}		
    }
    public void ExecDataInsert()
	{
		if(m_insertRow==null)
			return;
		int ii=0;
    <#assign i=0>
<#list entity.field as field>
<#if (field.@show?if_exists?size>0)>
     <#if (field.show?if_exists?size>0)>
		<#list field.show as show>
		${show.@entity_name?cap_first}Dao dao${i}=(SpringContext.Context.GetObject("${show.@entity_name?cap_first}Dao") as ${show.@entity_name?cap_first}Dao);
        ${show.@entity_name?cap_first} p${i} =  dao${i}.Get(dao${i}.DC.Add(Restrictions.Eq("${show.@ref}",${field.@name})));
		if(p${i}!=null)
			 m_insertRow(0,ii++,p${i}.${show.@refvalue}.ToString());
		else
			m_insertRow(0,ii++,<@fieldValueToString entity=entity field=field>${field.@name}</@fieldValueToString>);
		<#assign i=i+1>
		<#break>
		</#list>
     <#else>
     	m_insertRow(0,ii++,<@fieldValueToString entity=entity field=field>${field.@name}</@fieldValueToString>);
     </#if>
</#if>     
</#list>
    }
    public void ExecDataInsertByQuery(DetachedCriteria dc)
    {
      
         IList list=dao.List(dc); 
        
        if(m_insertHead!=null)
        {
			int ii=0;
			<#list entity.field as field>
	<#if (field.@show?if_exists?size>0)>
			m_insertHead(ii++,${field.@show},"${field.@label}");
	</#if>
			</#list>
		}		
		
      if(m_insertRow!=null)
      {
        int j=0;
        foreach(${entity.@entity_name?cap_first} obj in list)			
        {
		  int ii=0;
	     
<#assign i=0>
<#list entity.field as field>
<#if (field.@show?if_exists?size>0)>
     <#if (field.show?if_exists?size>0)>
		<#list field.show as show>
		${show.@entity_name?cap_first}Dao dao${i}=(SpringContext.Context.GetObject("${show.@entity_name?cap_first}Dao") as ${show.@entity_name?cap_first}Dao);
        ${show.@entity_name?cap_first} p${i} =  dao${i}.Get(dao${i}.DC.Add(Restrictions.Eq("${show.@ref}",obj.${field.@name})));
		if(p${i}!=null)
			 m_insertRow(j,ii++,p${i}.${show.@refvalue}.ToString());
		else
			m_insertRow(j,ii++,<#t><@fieldValueObjectToString entity=entity field=field>obj.${field.@name}</@fieldValueObjectToString>);
		<#assign i=i+1>
		<#break>
		</#list>
     <#else>
		m_insertRow(j,ii++,<#t><@fieldValueObjectToString entity=entity field=field>obj.${field.@name}</@fieldValueObjectToString>);	
     </#if>
</#if>     
</#list>
		j++;
        }
        

	
       }
    }

    
    public IList List(DetachedCriteria dc)
    {
        try
        {
           
            return dao.List(dc);
        }
        catch (Exception ex)
        {
            throw (new Exception(ex.Message));
        }
    }
    
    public bool Init(DetachedCriteria dc)
    {
    
    		${entity.@entity_name?cap_first} obj=dao.Get(dc);
    		if(obj==null)
    			return false;
    		else
    		{
    			__${entity.@entity_name}=obj;
            	return true;
            }
        
    }
    public static t_${entity.@entity_name} Get(DetachedCriteria dc)
    {
    	
    	${entity.@entity_name?cap_first} obj=((${entity.@entity_name?cap_first}Dao)SpringContext.Context.GetObject("${entity.@entity_name?cap_first}Dao"))
	 	.Get(dc);
	 	if(obj==null)
	 		return null;
	 	else
	 		return new t_${entity.@entity_name}( obj );
        
    }
<#list entity.field as field>
<#if (field.@unique?if_exists?size>0) && (field.@unique="yes") >
	 public static t_${entity.@entity_name} GetBy${field.@name?cap_first}(<@fieldToCSharpType field=field/> val)
	 {
	 	${entity.@entity_name?cap_first} obj=((${entity.@entity_name?cap_first}Dao)SpringContext.Context.GetObject("${entity.@entity_name?cap_first}Dao"))
	 	.GetBy${field.@name?cap_first}(val);
	 	if(obj==null)
	 		return null;
	 	else
	 		return new t_${entity.@entity_name}( obj );
	 	
	 	
	 }
	 public  bool InitBy${field.@name?cap_first}(<@fieldToCSharpType field=field/> val)
	 {
	 	

	 	${entity.@entity_name?cap_first} obj=dao.GetBy${field.@name?cap_first}(val) ;
	 	if(obj==null)
	 		return false;
	 	else
	 	{
	 		__${entity.@entity_name}=obj;
	 		return true;
	 	}
	 	
	 	
	 }
</#if>
</#list>
    
       public bool showInForm(Control.ControlCollection Controls)
    {
        try
        {
             
<#list entity.field as field>
		if(Controls["${field.@name}"]!=null)
		{
			Controls["${field.@name}"].Text=<@fieldValueToString entity=entity field=field>${field.@name}</@fieldValueToString>;
		}
</#list>
			return true;
        }
        catch (Exception ex)
        {
            throw (new Exception(ex.Message));
            
        }
    }
    public  t_${entity.@entity_name} Init(${entity.@entity_name?cap_first} item)
    {
        __${entity.@entity_name}=item;
        return this;
    }

    
     public bool initByForm(Control.ControlCollection Controls)
    {
        try
        {
             
<#list entity.field as field>
		if(Controls["${field.@name}"]!=null)
		{
			
			if(Controls["${field.@name}"].GetType().Name=="ComboBox")
			{
				ComboBox box=(ComboBox)Controls["${field.@name}"];
				
				string v=box.SelectedValue==null?box.Text:Convert.ToString(box.SelectedValue);
				try{${field.@name}=<@stringToFieldValue field=field>v</@stringToFieldValue>;}
				catch
				{
					 Controls["${field.@name}"].Focus();
					throw  (new Exception("${field.@label}格式不正确！！"));
				}
				
			}
			else
			{
				try{${field.@name}=<@stringToFieldValue field=field>Controls["${field.@name}"].Text</@stringToFieldValue>;}
				catch
				{
					 Controls["${field.@name}"].Focus();
					throw  (new Exception("${field.@label}格式不正确！！"));
				}
				
			}
				
				
		}
</#list>
			return true;
        }
        catch (Exception ex)
        {
            throw (new Exception(ex.Message));
            
        }
    }
    

}

}
<#break>
</#if>
</#list>